package com.zimin.cloud.user.biz.impl;

import cn.hutool.extra.spring.SpringUtil;
import com.alibaba.fastjson.JSON;
import com.zimin.cloud.mine.pojo.user.business.UserMomentsBO;
import com.zimin.cloud.mine.pojo.user.view.UserMomentsVO;
import com.zimin.cloud.user.common.constant.UserMomentsConstant;
import com.zimin.cloud.user.pojo.entity.UserMoments;
import com.zimin.common.annotation.Biz;
import com.zimin.cloud.user.biz.IUserMomentsBiz;
import com.zimin.cloud.user.service.impl.UserMomentsServiceImpl;
import com.zimin.common.utils.BeanUtils;
import com.zimin.redis.utils.RedisOperator;
import com.zimin.rocketmq.utils.RocketMQUtil;
import org.apache.rocketmq.client.exception.MQBrokerException;
import org.apache.rocketmq.client.exception.MQClientException;
import org.apache.rocketmq.client.producer.DefaultMQProducer;
import org.apache.rocketmq.common.message.Message;
import org.apache.rocketmq.remoting.exception.RemotingException;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;


/**
 * <p>
 * 用户动态表 业务对象
 * </p>
 *
 * @author zimin
 * @since 2022-06-14 15:53
 */
@Biz("userMomentsBiz")
public class UserMomentsBizImpl extends UserMomentsServiceImpl implements IUserMomentsBiz {

    @Autowired
    private RedisOperator redisOperator;


    @Override
    public boolean save(UserMomentsBO userMomentsBO) throws Exception{
        var userMoments = BeanUtils.coverClass(userMomentsBO, UserMoments.class);
        boolean flag = super.save(userMoments);
        DefaultMQProducer producer = SpringUtil.getBean("momentsProducer");
        var msg = new Message(UserMomentsConstant.TOPIC_MOMENTS, JSON.toJSONString(userMoments).getBytes());
        RocketMQUtil.syncSendMsg(producer, msg);
        return flag;
    }

    @Override
    public List<UserMomentsVO> getUserSubscribedMoments(String userId) {
        String key = "subscribed-" + userId;
        var listStr = redisOperator.get(key,String.class);
        return JSON.parseArray(listStr,UserMomentsVO.class);
    }
}
